﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.IO;

using ECCIT;
using ECCIT.Extension.Weibo_v2.Model;
using ECCIT.Extension.Weibo_v2.Common;

using OgilvyOne.UKNow.BLL;
using OgilvyOne.UKNow.Model;
using OgilvyOne.UKNow.Configuration;
using OgilvyOne.UKNow.Mobile.Model;
using OgilvyOne.UKNow.Mobile.Model.Events;
using OgilvyOne.UKNow.Mobile.Utilities;

namespace OgilvyOne.UKNow.Mobile.APIs.Calendar
{
    public class GetList : IHttpHandler
    {
        public bool IsReusable
        {
            get { return true; }
        }

        public void ProcessRequest(HttpContext context)
        {
            try
            {
                string sPageSize = context.Request.Params["page_size"];
                string sCurrPageIndex = context.Request.Params["current_page"];
                string sUid = context.Request.Params["uid"];
                string city = context.Request.QueryString["city"];
                int icity = 0;
                int.TryParse(city, out icity);

                if (string.IsNullOrEmpty(sUid)) throw new ArgumentNullException("uid");
                int pageSize = 10;
                if (!string.IsNullOrEmpty(sPageSize))
                    pageSize = int.Parse(sPageSize);

                int currPageIndex = 1;
                if (!string.IsNullOrEmpty(sCurrPageIndex))
                    currPageIndex = int.Parse(sCurrPageIndex);

                int uid = int.Parse(sUid);

                EventsBLL bll = new EventsBLL(2052);
                PagingList list = bll.GetMyEvents(uid, pageSize, currPageIndex,icity);
                List<EventInfo> events = new List<EventInfo>();
                List<int?> ids = new List<int?>();
                if (list.List != null)
                {
                    foreach (FavoriteEventsModel fem in list.List)
                        ids.Add(fem.COL_EVENTS_ID);
                    IList<EventsModel> models = bll.GetEventsModelsByEventsIDs(ids);

                    foreach (EventsModel model in models)
                    {
                        events.Add(ModelConvertUtility.ToEventInfo(model, false));
                    }
                   
                }

                ModelList<EventInfo> cei = new ModelList<EventInfo>(pageSize, currPageIndex, list.Capacity);
                cei.List = events;
                string s = JsonSerializationHelper.ObjectToJson(cei);
                context.Response.Write(ResultUtility.ReturnResultJson("0", s, "获取我的收藏活动列表成功"));
               
            }
            catch (Exception ex)
            {
                context.Response.Write(ResultUtility.ReturnResultJson("-100", "", "获取我的收藏活动列表失败. Message:" + ex.Message));
            }
        }
    }
}
